<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改页面</title>
    <script src="js/jquery-2.2.3.min.js"></script>
</head>
<body>
<form method="post" action="/applicant/saveEdit.do">
    <table>
        <input type="hidden" id="eid" name="eid" value=""/>

        <tr>
            <td>姓名：</td>
            <td><input name="ename" id="ename" value=""/></td>
        </tr>
        <tr>
            <td>性别：</td>
            <td><input type="radio" name="sex" id="man" value="男"/>男
                <input type="radio" name="sex" id="women" value="女"/>女
            </td>
        </tr>
        <tr>
            <td>学历：</td>
            <td><select id="education" name="education">

            </select>
            </td>
        </tr>
        <tr>
            <td>年龄：</td>
            <td><input name="age" id="age" value=""/></td>
        </tr>
        <tr>
            <td>联系电话：</td>
            <td><input name="phoneNum" id="phoneNum" value=""/></td>
        </tr>
        <tr>
            <td>邮箱：</td>
            <td><input name="email" id="email" value=""/></td>
        </tr>
        <tr>
            <td>面试官：</td>
            <td id="inter"></td>
        </tr>
        <tr>
            <td>状态：</td>
            <td><select id="status" name="status">
                <option>-请选择状态-</option>
            </select>
            </td>
        </tr>
        <tr>
            <td>备注：</td>
            <td><textarea name="textArea" id="textArea"></textarea></td>
        </tr>
    </table>
    <input type="submit" value="修改"/>
</form>
<span style="color: red"></span>
</body>
</html>

<script>
    //发送请求动态展示学历下拉菜单的数据
    findEducation()

    function findEducation() {
        $.ajax({
            url: "applicant/findEducation.do",
            success: function (resp) {
                $.each(resp, function (i, x) {
                    var option = `<option id="edu${i}" value="${x.education}">${x.education}</option>`
                    $("#education").append(option)
                })
            }
        })
    }

    //发送请求动态展示状态下拉菜单的数据
    findStatus()

    function findStatus() {
        $.ajax({
            url: "applicant/findStatus.do",
            success: function (resp) {
                $.each(resp, function (i, x) {
                    var option = `<option id="sta${i}" value="${x.status}">${x.statuStr}</option>`
                    $("#status").append(option)
                })
            }
        })
    }

    // 找到所有面试官并显示
    interviewer()

    function interviewer() {
        $.ajax({
            url: "interviewer/findAll.do",
            success: function (resp) {
                $.each(resp, function (i, v) {
                    var checkbox = `<input type="checkbox" id="box${i}" name="interviewerIds" value="${v.id}"/>${v.name}`
                    $("#inter").append(checkbox)
                })
            }
        })
    }

    //根据id找到用户做数据回显
    back()

    function back() {
        var id = location.search
        // 发送ajax请求数据
        $.ajax({
            url: "applicant/findById.do" + id,
            success: function (resp) {
                var names = ''
                $.each(resp.interviewers, function (m, n) {
                    names = n.name
                    if (names == '王强') {
                        $("#box0").attr('checked', 'checked')
                    }
                    if (names == '玛丽') {
                        $("#box1").attr('checked', 'checked')
                    }
                    if (names == '张三丰') {
                        $("#box2").attr('checked', 'checked')
                    }
                    if (names == '张无忌') {
                        $("#box3").attr('checked', 'checked')
                    }
                })
                $("#ename").val(resp.ename)
                if (resp.sex == '男') {
                    $("#man").attr("checked", 'checked')
                } else {
                    $("#women").attr("checked", 'checked')
                }
                $("#age").val(resp.age)
                if (resp.education == '专科') {
                    $("#edu0").attr('selected', 'selected')
                } else if (resp.education == '本科') {
                    $("#edu1").attr('selected', 'selected')
                } else if (resp.education == '研究生') {
                    $("#edu2").attr('selected', 'selected')
                }
                $("#phoneNum").val(resp.phoneNum)
                $("#email").val(resp.email)
                $("#textArea").val(resp.textArea)

                if (resp.status == 0) {
                    $("#sta0").attr('selected', 'selected')
                } else if (resp.status == 1) {
                    $("#sta1").attr('selected', 'selected')
                } else if (resp.status == 2) {
                    $("#sta2").attr('selected', 'selected')
                }
            }
        })
    }

    //找到隐藏input，将id值传递
    var eid = location.search.split("=")[1]
    $("#eid").val(eid)


    //修改结束点击修改提交参数修改应聘者数据
    //先拦截form表单自动提交
    saveEdit()

    function saveEdit() {
        $("form").submit(function (e) {
            e.preventDefault()
            // 序列化参数
            var params = $(this).serialize()
            $.ajax({
                url: "applicant/saveEdit.do",
                method: "post",
                data: params,
                success: function (resp) {
                    if (resp != 0) {
                        location.href = "list.html"
                    } else {
                        $("span").text("添加失败")
                    }
                }
            })
        })
    }

</script>